<?php
/* 
Plugin Name: WoW Armory 
Plugin URI: http://timsworld.nfshost.com 
Description: Shows your character's Armory info  
Version: 1.0
Author: Tim (SeiferTim) Hely
Author URI: http://timsworld.nfshost.com
*/  

function widget_wow_armory_stats_init() {
	add_action('admin_menu', 'wow_armory_menu');
	add_action('wp_head','wow_armory_headscript');
	wp_enqueue_script('jquery');
    wp_enqueue_script('wow_armory', get_bloginfo('wpurl') . '/wp-content/plugins/wow-armory/js/armory.js',array('jquery'),'1.0');    
	
	function widget_wow_armory($args) {
	    extract($args, EXTR_SKIP);
		echo $before_widget;
	    echo $before_title . 'Armory' . $after_title;	   
		echo '<div id="armory"><img src="'.get_bloginfo('wpurl').'/wp-content/plugins/wow-armory/ajax-loader.gif" /></div>';
	    echo $after_widget;
	}
	if ( function_exists('register_sidebar_widget') )
		register_sidebar_widget('WoW Armory','widget_wow_armory');
	
	function wow_armory_menu() {
	  add_options_page('WoW Armory', 'WoW Armory', 8, 'wowarmoryoptions', 'wow_armory_options');
	}
	
	function wow_armory_options() {
	  echo '<div class="wrap">';
	  echo '<h2>WoW Armory</h2>';
	  echo '<form method="post" action="options.php">';
	  wp_nonce_field('update-options');
	  echo '<table class="form-table">';
	  echo '<tr valign="top">';
	  echo '<th scope="row">Character Name</th>';
	  echo '<td><input type="text" name="armory_char_name" value="' . get_option('armory_char_name') .'"/></td>';
	  echo '</tr>';
	  echo '<tr>';
	  echo '<th scope="row">Location</td>';
	  echo '<td><select name="armory_realm_type"><option value="US"';
	  if (get_option('armory_realm_type') == 'US')
	  	echo 'selected';
	  echo '>US</option><option value="EU"';
	  if (get_option('armory_realm_type') == "EU")
	  	echo 'selected';
	  echo '>EU</option></select>';
	  echo '</tr>';
	  echo '<tr valign="top">';
	  echo '<th scope="row">Server</th>';
	  echo '<td><input type="text" name="armory_server" value="' . get_option('armory_server') . '"/></td>';
	  echo '</tr>';
	  echo '<tr>';
	  echo '<th scope="row">Show Gear?</th>';
	  echo '<td><input name="armory_show_gear" type="checkbox" value="armory_show_gear"'.(get_option('armory_show_gear') == TRUE ? ' checked ' : '').'/></td>';
	  echo '</tr>';	
	  echo '<tr>';
	  echo '<th scope="row">Show Professions?</th>';
	  echo '<td><input name="armory_show_professions" type="checkbox" value="armory_show_professions"'.(get_option('armory_show_professions') == TRUE ? ' checked ' : '').'/></td>';
	  echo '</tr>';
	  echo '<tr>';
	  echo '<th scope="row">Show Achievements?</th>';
	  echo '<td><input name="armory_show_achiev" type="checkbox" value="armory_show_achiev"'.(get_option('armory_show_achiev') == TRUE ? ' checked ' : '').'/></td>';
	  echo '</tr>';	  
	  echo '</table>';
	  echo '<input type="hidden" name="action" value="update" />';
	  echo '<input type="hidden" name="page_options" value="armory_char_name,armory_realm_type,armory_server,armory_show_gear,armory_show_professions,armory_show_achiev" />';
	  echo '<p class="submit">';  
	  echo '<input type="submit" name="Submit" value="Update" />';
	  echo '</p>';
	  echo '</form>';
	  echo '</div>';
	}
	
	function wow_armory_headscript(){
		echo '<script src="http://www.wowhead.com/widgets/power.js"></script>';
		echo '<script type="text/javascript">';
		echo 'jQuery(document).ready(function(){';
		echo "getArmory('". get_option('armory_char_name') ."','" . str_replace("'","\'",get_option('armory_server')) ."','" . get_option('armory_realm_type') . "','" . 	get_bloginfo('wpurl') ."','" . (get_option('armory_show_professions') == TRUE ? '1' : '0') . "','" . (get_option('armory_show_achiev') == TRUE ? '1' : '0') . "','" . (get_option('armory_show_gear') == TRUE ? '1' : '0') . "')";	
		echo '});';
		echo '</script>';
	}
}
add_action('plugins_loaded','widget_wow_armory_stats_init');
?>